﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.Library.Core;

namespace UITools
{
    public partial class TextureViewForm : Form
    {
        
        class TextureViewController
        {

            private PictureBox canvas;

            public TextureViewController(PictureBox c)
            {
                this.canvas = c;
                if (this.canvas.Image == null)
                {
                    this.canvas.Image = new Bitmap(this.canvas.ClientSize.Width,this.canvas.ClientSize.Height);
                }
            }

            public void Clean()
            {                
                using (var gs = Graphics.FromImage(this.canvas.Image))
                {
                    gs.FillRegion(Brushes.Black, new Region(this.canvas.ClientRectangle));
                }
            }

            public void SplatProvider(IRandomProvider rnd)
            {
                Clean();
                
                //using (var gs = Graphics.FromImage(this.canvas.Image))
                for (int i = 0; i < this.canvas.Image.Width*this.canvas.Image.Height;i++)
                {
                    int x = Math.Min((int) (this.canvas.Image.Width*rnd.NextFloat()), canvas.Image.Width - 1);
                    int y = Math.Min((int) (this.canvas.Image.Height*rnd.NextFloat()), canvas.Image.Height - 1);
                    (this.canvas.Image as Bitmap).SetPixel(x, y, 
                        //Color.FromArgb((int)(255 * rnd.NextFloat()), (int)(255 * rnd.NextFloat()), (int)(255 * rnd.NextFloat()))
                        Color.White
                        );
                }
            }

        }

        private TextureViewController controller;

        public TextureViewForm()
        {
            InitializeComponent();
            controller = new TextureViewController(this.ctlTexture);
        }

        private void splatUniformToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void uniformToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.controller.SplatProvider(new FastRandom());
            this.Refresh();
        }

        private void haltonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.controller.SplatProvider(new HaltonSequence());
            this.Refresh();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.controller.Clean();
            this.Refresh();
        }

    }
}
